import { SlateElement } from '@wangeditor/editor';
import { DavinciLink } from './custom-types';

function parseHtml(elem: SlateElement, children, editor): string {
	const { link, content } = elem as unknown as DavinciLink;
	const config = editor.getConfig();
	const { attrs = {} } = config.EXTEND_CONF?.['davinci-link'] ?? {};

	let attr = '';
	if (attrs) {
		for (const key in attrs) {
			attr += `${key}="${attrs[key]}" `;
		}
	}
	return `<a href="${link}" ${attr} data-w-e-type="davinci-link" data-w-e-is-vold data-w-e-is-inline >${content}</a>`;
}

export const elemToHtmlConf = {
	type: 'davinci-link',
	elemToHtml: parseHtml
};
